package com.ericbutler.info.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.ericbutler.info.domain.TestRecord;

@Scope("singleton")
@Service("testTableDAO")
public class TestTableDAOImpl extends jdbcDAO implements TestTableDAO {
	// SQL in file, cause fuck you
	private String insertSQL = "INSERT INTO TEST.TEST_TABLE_1 ( NAME, WORDS ) VALUES ( ?, ? )";
	private String getAllSQL = "SELECT * FROM TEST.TEST_TABLE_1";

	@Override
	public void insertTestRecord(String user, String text) {
		getDatasource().update(insertSQL, user, text);
	}

	@Override
	public List<TestRecord> getAllTestRecords() {
		return getDatasource().query(getAllSQL, new RowMapper<TestRecord>() {
			@Override
			public TestRecord mapRow(ResultSet rs, int index) throws SQLException {
				TestRecord newRecord = new TestRecord();
				newRecord.setId(rs.getString("ID"));
				newRecord.setUser(rs.getString("NAME"));
				newRecord.setText(rs.getString("WORDS"));
				newRecord.setUpdatedDate(rs.getDate("UPDATED_TIME"));
				return newRecord;
			}
		});
	}

}
